package cn.sangkf.litemall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.sangkf.litemall.product.entity.AttrEntity;
import cn.sangkf.litemall.product.service.AttrAttrgroupRelationService;
import cn.sangkf.litemall.product.service.AttrService;
import cn.sangkf.litemall.product.service.CategoryService;
import cn.sangkf.litemall.product.vo.AttrGroupRelationVo;
import cn.sangkf.litemall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.sangkf.litemall.product.entity.AttrGroupEntity;
import cn.sangkf.litemall.product.service.AttrGroupService;
import cn.sangkf.litemall.common.utils.PageUtils;
import cn.sangkf.litemall.common.utils.R;



/**
 * 属性分组
 *
 * @author sangkf
 * @email 413198756@qq.com
 * @date 2020-12-23 19:29:01
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 获取该分类下的所有属性分组
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 获取指定分类下的关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") long catelogId){
        List<AttrGroupWithAttrsVo> data = attrGroupService.getAttrGroupWithAttrs(catelogId);
        return R.ok().put("data", data);
    }

    /**
     * 获取该属性分组下的已关联的属性
     */
    @RequestMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> attrList = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", attrList);
    }

    /**
     * 获取指定属性分组下未关联的属性
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R noAttrRelation(@RequestParam Map<String, Object> params, @PathVariable("attrGroupId") Long attrGroupId){
        PageUtils page = attrService.getNoRelationAttr(params, attrGroupId);
        return R.ok().put("page", page);
    }

    /**
     * 保存属性分组和属性的关联关系
     * @param attrGroupRelationVos
     * @return
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos){
        attrAttrgroupRelationService.saveBatch(attrGroupRelationVos);
        return R.ok();
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] catelogPath = categoryService.findCatelogPath(attrGroup.getCatelogId());
		attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 删除属性关联
     */
    @RequestMapping("/attr/relation/delete")
    public R attrRelationDelete(@RequestBody AttrGroupRelationVo[] relationVos){
        attrService.deleteRelation(relationVos);

        return R.ok();
    }


}
